
using Microsoft.Practices.ServiceLocation;
using NCommon.NHibernate;

namespace Rhinestone.Domain.Mappings
{
    public class RhinestoneUserType : CompositeUserTypeBase<IUser>
    {
        #region .ctor
        public RhinestoneUserType()
        {
            MapProperty(prop => prop.Username);
        }
        #endregion

        #region Overrides of CompositeUserTypeBase<IUser>
        protected override IUser CreateInstance(object[] propertyValues)
        {
            var usersService = ServiceLocator.Current.GetInstance<IUsersService>();
            return usersService.FindUserByName((string) propertyValues[0]);
        }

        protected override IUser PerformDeepCopy(IUser source)
        {
            return source; //Users are immutable and cannot be changed.
        }

        public override bool IsMutable
        {
            get { return false; }
        }
        #endregion
    }
}